package service

import (
	"context"
	"github.com/google/uuid"
	"srv/basic/global"
	__ "srv/basic/proto"
	"srv/internal/model"
	"srv/pkg"
	"strings"
)

type Service struct {
	__.UnimplementedUserServer
}

func (s *Service) Combo(_ context.Context, req *__.ComboReq) (resp *__.ComboResp, err error) {
	var combos []model.Combo

	err = global.DB.Debug().Model(&model.Combo{}).Where("name = ?", req.Name).Find(&combos).Error
	if err != nil {
		return nil, err
	}

	var comboList []*__.Combo

	for _, combo := range combos {
		list := &__.Combo{
			Name:     combo.Name,
			Price:    float32(combo.Price),
			Duration: combo.Duration,
		}
		comboList = append(comboList, list)
	}

	resp = &__.ComboResp{List: comboList}

	return resp, err
}

func (s *Service) CreatOrder(_ context.Context, req *__.CreatOrderReq) (resp *__.CreatOrderResp, err error) {

	OrderSn := GetOrderSn()

	combo := model.Combo{}
	err = global.DB.Debug().Where("id = ?", req.ComboId).Find(&combo).Error
	if err != nil {
		return nil, err
	}

	user := model.User{}
	err = global.DB.Debug().Where("id = ?", req.UserId).Find(&user).Error
	if err != nil {
		return nil, err
	}

	recharge := model.RechargeLog{
		UserId:  req.UserId,
		Name:    combo.Name,
		Price:   combo.Price,
		OrderSn: OrderSn,
	}

	err = global.DB.Debug().Create(&recharge).Error
	if err != nil {
		return nil, err
	}

	pay := pkg.Pay(OrderSn, combo.Price)

	order := model.Order{
		UserId:  req.UserId,
		ComboId: req.ComboId,
		Amount:  combo.Price,
		OrderSn: OrderSn,
		Url:     pay,
		Status:  0,
	}

	err = global.DB.Debug().Create(&order).Error
	if err != nil {
		return nil, err
	}

	resp = &__.CreatOrderResp{
		OrderId: int64(order.ID),
		OrderSn: OrderSn,
		Url:     pay,
	}

	return resp, err

}

func GetOrderSn() string {
	str := uuid.NewString()
	uuidStr := strings.Replace(str, "_", "", 1)
	return uuidStr[:18]
}

func (s *Service) RechargeLog(_ context.Context, req *__.RechargeLogReq) (resp *__.RechargeLogResp, err error) {

	var rechargeLogs []model.RechargeLog

	err = global.DB.Debug().Model(&model.RechargeLog{}).Where("user_id = ?", req.UserId).Order("created_at Desc").Find(&rechargeLogs).Error
	if err != nil {
		return nil, err
	}

	var recharge []*__.RechargeLog

	for _, rechargeLog := range rechargeLogs {
		list := &__.RechargeLog{
			UserId:  rechargeLog.UserId,
			Name:    rechargeLog.Name,
			Price:   float32(rechargeLog.Price),
			OrderSn: rechargeLog.OrderSn,
		}
		recharge = append(recharge, list)
	}

	resp = &__.RechargeLogResp{List: recharge}

	return resp, err
}

func (s *Service) OrderSuccess(_ context.Context, req *__.OrderSuccessReq) (resp *__.OrderSuccessResp, err error) {

	order := model.Order{}

	err = global.DB.Debug().Where("order_sn = ?", req.OrderSn).Find(&order).Error
	if err != nil {
		return nil, err
	}

	if order.Status == 0 {
		err = global.DB.Debug().Model(&model.Order{}).Where("order_sn = ?", req.OrderSn).Update("status", 1).Error
		if err != nil {
			return nil, err
		}
	}
	resp = &__.OrderSuccessResp{Message: "更新权益成功"}
	
	return resp, err
}
